<template>
    <div>
        <el-card>
            <el-breadcrumb separator="/">
                <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>内容管理</el-breadcrumb-item>
                <el-breadcrumb-item>分类列表</el-breadcrumb-item>
            </el-breadcrumb>
            <div class="btnBox">
                <div>
                    <el-button size="small" @click="addHandle">添加</el-button>
                    <el-button size="small" icon="el-icon-upload2" @click="handle('up', 'top')">上线</el-button>
                    <el-button size="small" icon="el-icon-download" @click="handle('down', 'top')">下线</el-button>
                </div>
            </div>
            <el-table :data="tableData" border style="width: 100%" height="73vh" @selection-change="selectChange">
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column prop="categoryName" label="分类名"></el-table-column>
                <el-table-column label="状态">
                    <template slot-scope="scope">
                        <el-tag size="small" v-if="scope.row.categoryStatus == '1'">已上线</el-tag>
                        <el-tag size="small" type="warning" v-else>已下线</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="200">
                    <template slot-scope="scope">
                        <el-button size="small" icon="el-icon-upload2"
                            @click="handle('edit', 'table', scope.row)">修改</el-button>
                        <el-button size="small" icon="el-icon-upload2" v-if="scope.row.categoryStatus == '0'"
                            @click="handle('up', 'table', scope.row)">上线</el-button>
                        <el-button size="small" icon="el-icon-download" v-if="scope.row.categoryStatus == '1'"
                            @click="handle('down', 'table', scope.row)">下线</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        <!-- 添加弹窗 -->
        <el-dialog title="百科分类" :visible.sync="addialogVisible" width="30%" :before-close="closeAddDialog">
            <el-form :model="form" :inline="true" :rules="rules" ref="form" label-width="120px">
                <el-form-item label="分类名称" prop="categoryName">
                    <el-input size="small" v-model="form.categoryName" placeholder="请输入分类名称"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="closeAddDialog">取 消</el-button>
                <el-button type="primary" @click="submitAddDialog">确 定</el-button>
            </span>
        </el-dialog>


    </div>
</template>

<script>


import { addOrUpdateCategory, listCategory, updateCategoryStatus } from '../../network/content'


export default {
    data() {
        return {
            tableData: [],
            addialogVisible: false,//弹窗
            form: {
                categoryId: '',
                categoryName: '',
                categoryStatus: 1,
                companyId: sessionStorage.companyId,
            },
            rules: {
                categoryName: [
                    { required: true, message: '请输入分类名称', trigger: 'blur' },
                ],
            },
            selectList: [],// 选择的视频

            editFlag: false


        };
    },
    methods: {

        addHandle() {
            this.addialogVisible = true
        },

        getList() {
            listCategory({
                companyId: sessionStorage.companyId
            }).then(res => {
                if (res.flag) {
                    console.log(res);
                    this.tableData = res.data
                }
            })
        },

        handle(flag, position, obj) {
            if (position == 'top' && this.selectList.length == 0) {
                this.$message.warning('请选择要操作的数据')
                return
            }

            if (position == 'top') {
                var ids = '';
                this.selectList.forEach(ele => {
                    ids += ele.categoryId + ','
                })
                updateCategoryStatus({
                    ids,
                    status: flag == 'up' ? 1 : 0
                }).then(res => {
                    if (res.flag) {
                        this.$message.success('操作成功')
                        this.getList()
                    } else {
                        this.$message.warning('操作失败')
                    }
                })

            } else {
                if (flag == 'edit') {
                    this.editFlag = true
                    this.form = obj
                    console.log(this.form);
                    this.addialogVisible = true
                } else {
                    updateCategoryStatus({
                        ids: obj.categoryId,
                        status: flag == 'up' ? 1 : 0
                    }).then(res => {
                        if (res.flag) {
                            this.$message.success('操作成功')
                            this.getList()
                        } else {
                            this.$message.warning('操作失败')
                        }
                    })
                }

            }
        },
        selectChange(val) {
            this.selectList = val
        },

        submitAddDialog() {
            this.$refs.form.validate((valid) => {
                if (!valid) return
                var form = this.form;
                console.log(form);

                addOrUpdateCategory(form).then(res => {
                    if (res.flag) {
                        this.$message.success(this.editFlag ? '修改成功' : '添加成功')
                        this.getList()
                        this.closeAddDialog()
                    } else {
                        this.$message.warning('操作失败')
                    }
                })
            });

        },
        // 关闭弹窗
        closeAddDialog() {
            this.addialogVisible = false
            this.$refs.form.resetFields();
        },



    },
    created() {
        this.getList()
    },
    filters: {

    },
};
</script>

<style scoped></style>